import tkinter


# 函数
def display(i):  # 打印函数
    if i == '.' and len(show.get()) == 0:  # 如果第一个为小数点自动在前方加一个0
        show.insert('insert', string='0.')
    elif str(i) in '+x/%' and len(show.get()) == 0:  # 第一个禁止输入运算符(-除外，可作为负数)
        pass
    elif str(i) in '+-x/' and len(show.get()) >= 1 and show.get()[-1] in '+-x/':  # 禁止连续输入两次运算符
        back()
        show.insert('insert', string=str(i))
    else:
        show.insert('insert', string=str(i))


def clear():  # 全部清除
    show.delete(0, 'end')
    result.delete(0, 'end')


def back():  # 退格
    index = int(len(show.get()) - 1)
    show.delete(index, 'end')


def equal():  # 用eval进行运算求结果
    result.delete(0, 'end')
    show_str = show.get()
    if len(show_str) == 0:  # 没有输入结果为0
        show_str += '0'
    show_str = show_str.replace('x', '*')  # 乘法替换
    if show_str.endswith('%'):  # 百分号替换
        show_str = show_str.replace('%', '/100')
    else:
        show_str = show_str.replace('%', '/100*')  # 百分号替换时防止出现 /1006 情况，自动补齐乘号
    print(show_str)
    try:
        result_str = eval(show_str)
    except:
        result.insert(0, '错误')
    else:
        result.insert(0, result_str)


def keyboard(key):  # 绑定键盘事件
    if key.keysym == 'Delete':
        clear()
    elif key.keysym == 'BackSpace':
        back()
    elif key.keysym == 'Return':
        equal()
    elif key.keysym == 'plus':
        display('+')
    elif key.keysym == 'minus':
        display('-')
    elif key.keysym == 'asterisk':
        display('x')
    elif key.keysym == 'slash':
        display('/')
    elif key.keysym in '0123456789':
        display(key.keysym)


# 主窗口
root = tkinter.Tk()
root.geometry("365x700")
root.resizable(width=False, height=False)
root.configure(bg='#d2d4d9')
root.title("计算器")

# 上方展示框
show = tkinter.Entry(root, font=('等线', 28, 'bold'), bg='#d2d4d9', bd=0)
show.configure(insertbackground='#1254e9')
show.place(y=50, height=100, width=365)
# 计算结果框
result = tkinter.Entry(root, font=('等线', 22, 'bold'), bg='#d2d4d9', bd=0)
result.place(y=150, height=80, width=365)

# 点击部分
bottom = tkinter.Label(root, bg='#f3f4f6')
bottom.place(y=250, width=365, height=490)

# 按钮部分第一行
btn_clear = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', fg='#0d54f8', text='C', border=0,
                           activebackground='#c8c8c8', activeforeground='#083395', command=clear)
btn_clear.place(x=20, y=290, height=65, width=65)
btn_div = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', fg='#0d54f8', text='/', border=0,
                         activebackground='#c8c8c8', activeforeground='#083395', command=lambda: display('/'))
btn_div.place(x=105, y=290, height=65, width=65)
btn_mul = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', fg='#0d54f8', text='x', border=0,
                         activebackground='#c8c8c8', activeforeground='#083395', command=lambda: display('x'))
btn_mul.place(x=190, y=290, height=65, width=65)
img = tkinter.PhotoImage(file='back.png')
btn_back = tkinter.Button(root, bg='#f9f9f9', fg='#0d54f8', border=0,
                          activebackground='#c8c8c8', command=back, image=img)
btn_back.place(x=275, y=290, height=65, width=65)

# 按钮部分第二行
btn_num7 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='7', activebackground='#c8c8c8',
                          command=lambda: display(7))
btn_num7.place(x=20, y=375, height=65, width=65)
btn_num8 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='8', activebackground='#c8c8c8',
                          command=lambda: display(8))
btn_num8.place(x=105, y=375, height=65, width=65)
btn_num9 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='9', activebackground='#c8c8c8',
                          command=lambda: display(9))
btn_num9.place(x=190, y=375, height=65, width=65)
btn_sub = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', fg='#0d54f8', text='-', border=0,
                         activebackground='#c8c8c8', activeforeground='#083395', command=lambda: display('-'))
btn_sub.place(x=275, y=375, height=65, width=65)

# 按钮部分第三行
btn_num4 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='4', activebackground='#c8c8c8',
                          command=lambda: display(4))
btn_num4.place(x=20, y=460, height=65, width=65)
btn_num5 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='5', activebackground='#c8c8c8',
                          command=lambda: display(5))
btn_num5.place(x=105, y=460, height=65, width=65)
btn_num6 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='6', activebackground='#c8c8c8',
                          command=lambda: display(6))
btn_num6.place(x=190, y=460, height=65, width=65)
btn_add = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', fg='#0d54f8', text='+', border=0,
                         activebackground='#c8c8c8', activeforeground='#083395', command=lambda: display('+'))
btn_add.place(x=275, y=460, height=65, width=65)

# 按钮部分第四行
btn_num1 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='1', activebackground='#c8c8c8',
                          command=lambda: display(1))
btn_num1.place(x=20, y=545, height=65, width=65)
btn_num2 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='2', activebackground='#c8c8c8',
                          command=lambda: display(2))
btn_num2.place(x=105, y=545, height=65, width=65)
btn_num3 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='3', activebackground='#c8c8c8',
                          command=lambda: display(3))
btn_num3.place(x=190, y=545, height=65, width=65)
btn_result = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#0b58f4', fg='#f9ffff', text='=', border=0,
                            activebackground='#073592', activeforeground='#f9ffff', command=equal)
btn_result.place(x=275, y=545, height=150, width=65)

# 按钮部分第五行
img_percent = tkinter.PhotoImage(file='percent.png')
btn_percent = tkinter.Button(root, bg='#f9f9f9', border=0, image=img_percent,
                             activebackground='#c8c8c8', command=lambda: display('%'))
btn_percent.place(x=20, y=630, height=65, width=65)
btn_num0 = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='0', activebackground='#c8c8c8',
                          command=lambda: display(0))
btn_num0.place(x=105, y=630, height=65, width=65)
btn_point = tkinter.Button(root, font=('等线', 24, 'bold'), bg='#f9f9f9', border=0, text='.',
                           activebackground='#c8c8c8', command=lambda: display('.'))
btn_point.place(x=190, y=630, height=65, width=65)

root.bind('<Key>', keyboard)
root.mainloop()
